package com.stars.module.setting.controller;

import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.base.exception.BaseException;
import com.stars.module.setting.entity.SystemDefine;
import com.stars.module.setting.service.SystemDefineService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : Rocks
 * @version : 1.0
 * @date : 2023/1/30 22:41
 * @description : 系统设置配置控制器
 */
@RestController
@RequestMapping("/starDefine")
public class SystemDefineController {

    private final SystemDefineService defineService;

    public SystemDefineController(SystemDefineService defineService) {
        this.defineService = defineService;
    }

    /**
     * 根据查询条件加载系统设置配置列表
     * @author : Rocks
     * @date :  2023/2/26 21:09
     * @param dto 查询条件
     * @return Result
     */
    @RequestMapping("/getDefineList")
    public Result loadDefineList(@RequestBody CommonDTO dto) {
        return Result.success(defineService.loadDefineList(dto));
    }

    /**
     * 保存系统设置配置
     * @author : Rocks
     * @date :  2023/2/26 21:10
     * @param entity 系统设置配置实体
     * @return Result
     */
    @RequestMapping("/save")
    public Result save(@RequestBody SystemDefine entity) {
        try {
            boolean result = defineService.saveOrUpdate(entity);
            return Result.result(result);
        } catch (BaseException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取系统设置配置最大排序
     * @author : Rocks
     * @date :  2023/2/26 21:10
     * @return Result
     */
    @RequestMapping("/getMaxIndex")
    public Result getMaxIndex() {
        return Result.success(defineService.getMaxIndex());
    }

    /**
     * 根据系统设置配置ID获取系统设置配置对象
     * @author : Rocks
     * @date :  2023/2/26 21:11
     * @param id 系统设置配置ID
     * @return Result
     */
    @RequestMapping("/getDefineById")
    public Result getDefineById(String id) {
        return Result.success(defineService.getDefineById(id));
    }

    /**
     * 删除系统设置配置
     * @author : Rocks
     * @date :  2023/2/26 21:11
     * @param id 要删除的系统设置配置ID
     * @return Result
     */
    @RequestMapping("/removeDefine")
    public Result removeDefine(String id) {
        return Result.result(defineService.removeDefine(id));
    }
}
